<template>
<el-dialog :title="!dataForm.id ? '新建' :'编辑'"
           :close-on-click-modal="false" append-to-body
           :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll
           width="1000px">
<el-row :gutter="15" class="">
<el-form ref="elForm" :model="dataForm" :rules="rules" size="medium" label-width="100px" label-position="right" >
    <template v-if="!loading">
            <el-col :span="8"  >
                <el-form-item  label="项目"  
 prop="project" >
                    <el-select   v-model="dataForm.project"
 placeholder="请选择"  clearable  :style='{"width":"100%"}'>
                            <el-option v-for="(item, index) in projectOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="关联资产"  
 prop="zcId" >
                    <el-select   v-model="dataForm.zcId"
 placeholder="请选择"  clearable  :style='{"width":"100%"}'>
                            <el-option v-for="(item, index) in zcIdOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="设备名称"  
 prop="equipmentName" >
                    <el-select   v-model="dataForm.equipmentName"
 placeholder="请选择"  clearable  :style='{"width":"100%"}'>
                            <el-option v-for="(item, index) in equipmentNameOptions" :key="index" :label="item.fullName" :value="item.id" :disabled="item.disabled" ></el-option>

                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="单价 元/度"  
 prop="price" >
                    <el-input   v-model="dataForm.price"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="设备倍率"  
 prop="equipmentPower" >
                    <el-input   v-model="dataForm.equipmentPower"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
            <el-divider   content-position="center" >
上期信息         </el-divider>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="上期读数"  
 prop="previousReading" >
                    <el-input   v-model="dataForm.previousReading"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="上期抄录时间"  
 prop="previousTime" >
                    <el-date-picker   v-model="dataForm.previousTime"
 placeholder="请选择"  clearable  :style='{"width":"100%"}' type="date"  format="yyyy-MM-dd"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
            <el-divider   content-position="center" >
本期读数         </el-divider>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="本期读数"  
 prop="currentReading" >
                    <el-input   v-model="dataForm.currentReading"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="本期用量"  
 prop="currentUsage" >
                    <el-input   v-model="dataForm.currentUsage"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="8"  >
                <el-form-item  label="本期费用"  
 prop="currentCost" >
                    <el-input   v-model="dataForm.currentCost"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
            <el-divider   content-position="center" >
抄录信息         </el-divider>
            </el-col>
            <el-col :span="10"  >
                <el-form-item  label="抄录时间"  
 prop="currentTime" >
                    <el-date-picker   v-model="dataForm.currentTime"
 placeholder="请选择"  clearable  :style='{"width":"100%"}' type="date"  format="yyyy-MM-dd"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="14"  >
                <el-form-item  label="抄录照片"  
 prop="fileAddress" >
                    <JNPF-UploadImg   v-model="dataForm.fileAddress"
 :fileSize="10"    sizeUnit="MB"  :limit="9" >

                </JNPF-UploadImg>
                </el-form-item>
            </el-col>
    </template>
</el-form>
    <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
              ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
    </el-row>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false"> 取 消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
    </span>
    </el-dialog>
</template>
<script>
    import request from '@/utils/request'
    import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'

    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                loading: false,
                btnLoading: false,
                selectDialogVisible: false,
                currTableConf:{},
                addTableConf:{
            },
                tableRows:{
                },
            currVmodel:"",
            dataForm: {
                                project : "",
                                zcId : "",
                                equipmentName : "",
                                    price : '',
                                    equipmentPower : '',
                                    previousReading : '',
                                    previousTime : '',
                                    currentReading : '',
                                    currentUsage : '',
                                    currentCost : '',
                                    currentTime : '',
                                fileAddress : [],
            },
            rules:
            {
                            project: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            zcId: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            equipmentName: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            currentReading: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            currentTime: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],



            },
                        projectOptions:[{"fullName":"选项一","id":"1"},{"fullName":"选项二","id":"2"}],
                        zcIdOptions:[{"fullName":"选项一","id":"1"},{"fullName":"选项二","id":"2"}],
                        equipmentNameOptions:[{"fullName":"选项一","id":"1"},{"fullName":"选项二","id":"2"}],

        }
        },
        computed: {},
        watch: {},
        created() {
        },
        mounted() {},
        methods: {
            clearData(){
                               this.dataForm.project = "";
                               this.dataForm.zcId = "";
                               this.dataForm.equipmentName = "";
                               this.dataForm.price = '';
                               this.dataForm.equipmentPower = '';
                               this.dataForm.previousReading = '';
                               this.dataForm.previousTime = '';
                               this.dataForm.currentReading = '';
                               this.dataForm.currentUsage = '';
                               this.dataForm.currentCost = '';
                               this.dataForm.currentTime = '';
                                 this.dataForm.fileAddress = [];
            },
            init(id) {
                this.dataForm.id = id || 0;
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/PropertyMeter/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        });
                    }else{
                       this.clearData()
                    }
                });
                this.$store.commit('generator/UPDATE_RELATION_DATA', {})
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                            this.request()
                    }
                })
            },
            request() {
                this.btnLoading = true
                let _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/PropertyMeter',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }else{
                    request({
                        url: '/api/example/PropertyMeter/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }
            },
            openSelectDialog(key) {
                this.currTableConf=this.addTableConf[key]
                this.currVmodel=key
                this.selectDialogVisible = true
                this.$nextTick(() => {
                    this.$refs.selectDialog.init()
                })
            },
            addForSelect(data) {
                for (let i = 0; i < data.length; i++) {
                    let item={...this.tableRows[this.currVmodel],...data[i]}
                    this.dataForm[this.currVmodel].push(item)
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));
        _data.fileAddress = Array.isArray(_data.fileAddress)? JSON.stringify(_data.fileAddress):'[]'
                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
        _dataAll.fileAddress = _dataAll.fileAddress ?JSON.parse(_dataAll.fileAddress):[]
                this.dataForm = _dataAll
            },
        },
    }

</script>
